import Vue from "vue";
import VueRouter from "vue-router";
import Login from "@/views/login";
import Layout from "@/views/layout";
import LayoutIndexImg from "@/views/layoutIndexImg";
import UserList from "@/views/userList";
import RolesList from "@/views/rolesList";
import { getToken } from "@/utils/storage";
Vue.use(VueRouter);

const routes = [
  {
    path: "/login",
    component: Login,
  },
  {
    path: "/",
    component: Layout,
    redirect: "layoutIndexImg",
    children: [
      {
        path: "layoutIndexImg",
        component: LayoutIndexImg,
      },
      {
        path: "userList",
        component: UserList,
      },
      {
        path: "rolesList",
        component: RolesList,
      },
    ],
  },
];

const router = new VueRouter({
  routes,
});
//获取token
const token = getToken();
//全局路由首位 有token可以直接进入 没有token并且进入的页面不是登录页面，就跳转到登录页面
router.beforeEach((to, from, next) => {
  if (to.path !== "/login" && !token) return next("/login");
  next();
});
export default router;
